/******************************************************************************
 *                  Shanghai ChipON Micro-Electronic Co.,Ltd                  *
 ******************************************************************************
 *  $File Name$       : Usart.c                                         	  *
 *  $Author$          : ChipON AE/FAE Group                                   *
 *  $Data$            : 2021-07-8                                             *
 *  $AutoSAR Version  : V1.0	                                              *
 *  $Description$     : This file provides the serial routine related         *
 *                     configuration functions, including:                    *
 *                          + Initializes the GPIO of the Usart               *
 *                          + Usart to send data							  *
 *                          + Configure USART mode                            *
 *                          + Configuration USART Initialize	              *
 ******************************************************************************
 *  Copyright (C) by Shanghai ChipON Micro-Electronic Co.,Ltd                 *
 *  All rights reserved.                                                      *
 *                                                                            *
 *  This software is copyrght protected and proprietary to                    *
 *  Shanghai ChipON Micro-Electronic Co.,Ltd.                                 *
 ******************************************************************************
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *                     		REVISON HISTORY                               	  *
 *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  Data       Version  Author        Description                             *
 *  ~~~~~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  *
 *  2021-07-08 00.01.00 FAE Group     new creat                               *
 *                                                                            *
 *                                                                            *
 *****************************************************************************/

/******************************************************************************
*                        		Include Files                                 *
******************************************************************************/
#include "system_init.h"
#include "Usart.h"
#include "Usart_Config.h"
/*******************************************************************************
**                     		   Global Functions 		             	      **
*******************************************************************************/
/**
 *  @brief :Initializes the GPIO of the Usart
 *  @param in :None
 *  @param out :None
 *  @retval :PE1 -- USART1_TX0
 *           PE0 -- USART1_RX
 */
void Usart_Gpio_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	/*
	*  Configure PE1 remap mode
	*  as TX pin
	*/
	GPIO_InitStructure.m_Mode = GPIO_MODE_RMP;
	GPIO_InitStructure.m_OpenDrain = GPIO_POD_PP;
	GPIO_InitStructure.m_PullDown = GPIO_NOPULL;
	GPIO_InitStructure.m_PullUp = GPIO_NOPULL;
	GPIO_InitStructure.m_Speed = GPIO_LOW_SPEED;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_1;
	GPIO_Configuration(GPIOE_SFR, &GPIO_InitStructure);
	/*
	* Configure PE0 remap mode
	* as RX pin
	* Configured in pull-up mode
	*/
	GPIO_InitStructure.m_PullUp = GPIO_PULLUP;
	GPIO_InitStructure.m_Pin = GPIO_PIN_MASK_0;
	GPIO_Configuration(GPIOE_SFR, &GPIO_InitStructure);
	/* Configure PA3 remap function to AF3 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_1, GPIO_RMP_PG1_AF11);
	/* Configure PE17 remap function to AF3 */
	GPIO_Pin_RMP_Config(GPIOE_SFR, GPIO_Pin_Num_0, GPIO_RMP_PG1_AF11);
}

/**
 *  @brief :Usart to send data
 *  @param in :
 *              USARTx :A pointer to the USART memory structure
 *                      with a value of USART0_SFR
 *                                      USART1_SFR
 *                                      USART2_SFR
 *                                      USART5_SFR
 *  			Usart_Sdu: Data   : A pointer to the sending data
 *  			           Length : Length of transmission
 *  @param out :None
 *  @retval :None
 */

void USART_Send(USART_SFRmap *USARTx, uint8_t *Databuf, uint32_t length)
{
	for (uint32_t i = 0; i < length; i++)
	{
		/* Serial transmission */
		USART_SendData(USARTx, Databuf[i]);
	}
}

/**
 *  @brief :Configure USART mode
 *  @param in :
 *             USARTx :A pointer to the USART memory structure
 *                     with a value of USART0_SFR
 *                                     USART1_SFR
 *                                     USART2_SFR
 *                                     USART5_SFR
 *  @param out :None
 *  @retval :None
 */
void USART_Mode_Config(USART_SFRmap *USARTx)
{
	USART_InitTypeDef USART_InitStructure;
	USART_Struct_Init(&USART_InitStructure);
	/* Set Usart To Async Mode */
	USART_InitStructure.m_Mode = USART_MODE_FULLDUPLEXASY;
	USART_InitStructure.m_TransferDir = USART_DIRECTION_FULL_DUPLEX;
	USART_InitStructure.m_WordLength = USART_WORDLENGTH_8B;
	USART_InitStructure.m_StopBits = USART_STOPBITS_1;

	/*  Set baudrate to 115200 */
	USART_InitStructure.m_BaudRateBRCKS = USART_CLK_HFCLK;
	USART_InitStructure.m_BaudRateInteger = 8;
	USART_InitStructure.m_BaudRateNumerator = 1;
	USART_InitStructure.m_BaudRateDenominator = 13;
	/* Set Usart to send enable */
	USART_Reset(USARTx);
	USART_Configuration(USARTx, &USART_InitStructure);
	USART_Passageway_Select_Config(USARTx, USART_U7816R_PASSAGEWAY_TX0);
	USART_RESHD_Enable(USARTx, TRUE);
	USART_Transmit_Data_Enable(USARTx, TRUE);
	USART_Cmd(USARTx, TRUE);
}

/**
 *  @brief :Configuration USART Initialize
 *  @param in :
 *         USARTx :A pointer to the USART memory structure
 *                 with a value of USART0_SFR
 *                                 USART1_SFR
 *                                 USART2_SFR
 *                                 USART5_SFR
 *		   Peripheral :Interrupt vector number
 *		              with a value of INT_USART0
 *		                              INT_USART1
 *		                              INT_USART2
 *		                              INT_USART5
 *  @param out :None
 *  @retval :None
 */
void USART_Int_Config(USART_SFRmap *USARTx, InterruptIndex Peripheral)
{
	/* Resetting USARTx sends the BUF interrupt bit */
	USART_Receive_Data_Enable(USARTx, TRUE);
	/* SET USARTx RDR interrupt enable */
	USART_RDR_INT_Enable(USARTx, TRUE);
	/* Configure USART to receive idle frame interrupt flag generation mode */
	USART_Receive_Idle_Frame_Config(USARTx, TRUE);
	/* SET USARTx IDLE interrupt enable */
	USART_IDLE_INT_Enable(USARTx, TRUE);
	/*Interrupt enables a peripheral or kernel interrupt vector */
	INT_Interrupt_Enable(Peripheral, TRUE);
}
